public class OpportunityExtensionVF {

    private final Opportunity[] opportunity = new Opportunity[1];
    private ID oppId = null;
    public List<Opportunity> opportunityList {get; set;}
    public Double expectedCredit {get; set;}   
    public Double ytdAttainment {get; set;}    
    public Double commissionRate {get; set;}    
    public Double commissionAmount {get; set;} 
    public Boolean commissionAmountFlag {get; set;}
    public Boolean commissionRateFlag {get; set;}
    public Boolean ytdAttainmentFlag {get; set;}
    public String comments {get; set;}  
    public String claimStatus {get; set;}  

    private Claim__c existingClaim = new Claim__c();
    public OpportunityExtensionVF(ApexPages.StandardController myController)
    {
        opportunity[0] = (Opportunity)myController.getRecord();
        oppId = opportunity[0].id;//[select Opportunity__c from Claim__c where id = : opportunity[0].id].Opportunity__c;
        //oppId = ApexPages.currentPage().getParameters().get('id');
        opportunityList = [select Name, Amount, AccountId, Opportunity.Account.Name, ExpectedRevenue, Type, Description, OwnerId, Opportunity.Owner.Name, CloseDate, LeadSource, Opportunity.Campaign.Name, MainCompetitors__c, OrderNumber__c, CurrentGenerators__c, TrackingNumber__c from Opportunity where id = :oppId];
        ytdAttainment = 0.0;
        //ytdAttainmentFlag = commissionRateFlag = commissionAmountFlag = true;
        
        claimObj();
    }

    
    public List<Opportunity> getOpportunityDetails()
    {
        return opportunityList;
    }
    
    public PageReference calculate()
    {
        if(commissionRate != null)
            commissionAmount = (commissionRate * expectedCredit) / 100;
        return null;
    }

    public PageReference lookupPlanDocument()
    {
        return null;
    }

    public PageReference fileClaim()
    {
        Claim__c claim = new Claim__c(Attainment__c = ytdAttainment, Commission_Rate__c = commissionRate, Commission_Amount__c = commissionAmount, Credit__c = expectedCredit, Opportunity__c = opportunity[0].id, Comments__c = comments, Claim_Status__c = 'Pending Approval');
        insert claim;
        Approval.ProcessSubmitRequest request =new Approval.ProcessSubmitRequest();
        request.setObjectId(claim.Id);
        Approval.ProcessResult result = Approval.process(request);
        
        return null;
    }

    private void claimObj()
    {
        Claim__c[] claimObj = [select  id, Claim__c.Opportunity__r.Amount, Commission_Amount__c, Commission_Rate__c, Attainment__c, Credit__c, Comments__c, Claim_Status__c from Claim__c where Opportunity__c = :oppId];
        if(claimObj.size() > 0)
        {
            expectedCredit = claimObj[0].Opportunity__r.Amount;
            ytdAttainment = claimObj[0].Attainment__c;
            commissionRate = claimObj[0].Commission_Rate__c;
            commissionAmount = claimObj[0].Commission_Amount__c;
            ytdAttainmentFlag = commissionRateFlag = commissionAmountFlag = true;
            claimStatus = claimObj[0].Claim_Status__c;
            
        }
        else
        {
            Plan__c[] plansList = [select id from Plan__c where Start_Date__c <= :opportunityList[0].CloseDate and End_Date__c >= :opportunityList[0].CloseDate];
            if(plansList.size() > 0)
            {
                Rate__c[] ratesList = [select Rate__c from  Rate__c where Plan__c = :plansList[0].id];
//                Plan_Role__c pRolesList = [select Id from Plan_Role__c where ]
                
                expectedCredit = opportunityList[0].Amount;
                commissionRate = ratesList[0].Rate__c;
                claimStatus = 'Draft';
                
            }
        }
    }
    
/*    public PageReference deleteTempClaim()
    {
        if(toBeDeletedClaim != null)
            delete toBeDeletedClaim;
        toBeDeletedClaim = null;
        return null;
    }
  */  
    public Claim__c getClaimData()
    {
        if(existingClaim != null)
            return existingClaim;
    //    else
      //      return toBeDeletedClaim;        
        return null;
    }

}